In [72]:
!pip install yfinance
!pip install bs4
!pip install nbformat
!pip install --upgrade plotly
Requirement already satisfied: yfinance in /opt/conda/lib/python3.12/site-packages (0.2.55)
Requirement already satisfied: pandas>=1.3.0 in /opt/conda/lib/python3.12/site-packages (from yfinance) (2.2.3)
Requirement already satisfied: numpy>=1.16.5 in /opt/conda/lib/python3.12/site-packages (from yfinance) (2.2.4)
Requirement already satisfied: requests>=2.31 in /opt/conda/lib/python3.12/site-packages (from yfinance) (2.32.3)
Requirement already satisfied: multitasking>=0.0.7 in /opt/conda/lib/python3.12/site-packages (from yfinance) (0.0.11)
Requirement already satisfied: platformdirs>=2.0.0 in /opt/conda/lib/python3.12/site-packages (from yfinance) (4.3.6)
Requirement already satisfied: pytz>=2022.5 in /opt/conda/lib/python3.12/site-packages (from yfinance) (2024.2)
Requirement already satisfied: frozendict>=2.3.4 in /opt/conda/lib/python3.12/site-packages (from yfinance) (2.4.6)
Requirement already satisfied: peewee>=3.16.2 in /opt/conda/lib/python3.12/site-packages (from yfinance) (3.17.9)
Requirement already satisfied: beautifulsoup4>=4.11.1 in /opt/conda/lib/python3.12/site-packages (from yfinance) (4.12.3)
Requirement already satisfied: soupsieve>1.2 in /opt/conda/lib/python3.12/site-packages (from beautifulsoup4>=4.11.1->yfinance) (2.5)
Requirement already satisfied: python-dateutil>=2.8.2 in /opt/conda/lib/python3.12/site-packages (from pandas>=1.3.0->yfinance) (2.9.0.post0)
Requirement already satisfied: tzdata>=2022.7 in /opt/conda/lib/python3.12/site-packages (from pandas>=1.3.0->yfinance) (2025.2)
Requirement already satisfied: charset_normalizer<4,>=2 in /opt/conda/lib/python3.12/site-packages (from requests>=2.31->yfinance) (3.4.1)
Requirement already satisfied: idna<4,>=2.5 in /opt/conda/lib/python3.12/site-packages (from requests>=2.31->yfinance) (3.10)
Requirement already satisfied: urllib3<3,>=1.21.1 in /opt/conda/lib/python3.12/site-packages (from requests>=2.31->yfinance) (2.3.0)
Requirement already satisfied: certifi>=2017.4.17 in /opt/conda/lib/python3.12/site-packages (from requests>=2.31->yfinance) (2024.12.14)
Requirement already satisfied: six>=1.5 in /opt/conda/lib/python3.12/site-packages (from python-dateutil>=2.8.2->pandas>=1.3.0->yfinance) (1.17.0)
Requirement already satisfied: bs4 in /opt/conda/lib/python3.12/site-packages (0.0.2)
Requirement already satisfied: beautifulsoup4 in /opt/conda/lib/python3.12/site-packages (from bs4) (4.12.3)
Requirement already satisfied: soupsieve>1.2 in /opt/conda/lib/python3.12/site-packages (from beautifulsoup4->bs4) (2.5)
Requirement already satisfied: nbformat in /opt/conda/lib/python3.12/site-packages (5.10.4)
Requirement already satisfied: fastjsonschema>=2.15 in /opt/conda/lib/python3.12/site-packages (from nbformat) (2.21.1)
Requirement already satisfied: jsonschema>=2.6 in /opt/conda/lib/python3.12/site-packages (from nbformat) (4.23.0)
Requirement already satisfied: jupyter-core!=5.0.*,>=4.12 in /opt/conda/lib/python3.12/site-packages (from nbformat) (5.7.2)
Requirement already satisfied: traitlets>=5.1 in /opt/conda/lib/python3.12/site-packages (from nbformat) (5.14.3)
Requirement already satisfied: attrs>=22.2.0 in /opt/conda/lib/python3.12/site-packages (from jsonschema>=2.6->nbformat) (25.1.0)
Requirement already satisfied: jsonschema-specifications>=2023.03.6 in /opt/conda/lib/python3.12/site-packages (from jsonschema>=2.6->nbformat) (2024.10.1)
Requirement already satisfied: referencing>=0.28.4 in /opt/conda/lib/python3.12/site-packages (from jsonschema>=2.6->nbformat) (0.36.2)
Requirement already satisfied: rpds-py>=0.7.1 in /opt/conda/lib/python3.12/site-packages (from jsonschema>=2.6->nbformat) (0.22.3)
Requirement already satisfied: platformdirs>=2.5 in /opt/conda/lib/python3.12/site-packages (from jupyter-core!=5.0.*,>=4.12->nbformat) (4.3.6)
Requirement already satisfied: typing-extensions>=4.4.0 in /opt/conda/lib/python3.12/site-packages (from referencing>=0.28.4->jsonschema>=2.6->nbformat) (4.12.2)
Requirement already satisfied: plotly in /opt/conda/lib/python3.12/site-packages (6.0.1)
Requirement already satisfied: narwhals>=1.15.1 in /opt/conda/lib/python3.12/site-packages (from plotly) (1.32.0)
Requirement already satisfied: packaging in /opt/conda/lib/python3.12/site-packages (from plotly) (24.2)
In [73]:
import yfinance as yf
import pandas as pd
import requests
from bs4 import BeautifulSoup
import plotly.graph_objects as go
from plotly.subplots import make_subplots

Question 1¶

In [74]:
tesla = yf.Ticker("TSLA")
In [75]:
tesla_data= tesla.history(period="max")
In [76]:
tesla_data.reset_index(inplace=True)

tesla_data.head()
Out[76]:
Date Open High Low Close Volume Dividends Stock Splits
0 2010-06-29 00:00:00-04:00 1.266667 1.666667 1.169333 1.592667 281494500 0.0 0.0
1 2010-06-30 00:00:00-04:00 1.719333 2.028000 1.553333 1.588667 257806500 0.0 0.0
2 2010-07-01 00:00:00-04:00 1.666667 1.728000 1.351333 1.464000 123282000 0.0 0.0
3 2010-07-02 00:00:00-04:00 1.533333 1.540000 1.247333 1.280000 77097000 0.0 0.0
4 2010-07-06 00:00:00-04:00 1.333333 1.333333 1.055333 1.074000 103003500 0.0 0.0

Question 2¶

In [77]:
url = "https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-PY0220EN-SkillsNetwork/labs/project/revenue.htm"
In [78]:
html_data= requests.get(url).text
In [79]:
soup = BeautifulSoup(html_data, 'html.parser')

soup.find_all('title')
Out[79]:
[<title>Tesla Revenue 2010-2022 | TSLA | MacroTrends</title>]
In [80]:
tesla_dict = {}
tesla_dict["Date"]=[]
tesla_dict["Revenue"]=[]
In [103]:
table = soup.select("table")[1]
rows = table.select("tbody > tr")
for row in rows:
    col = row.find_all("td")
    if len(col) >= 2:
        tesla_date = col[0].text
        tesla_revenue = col[1].text
        tesla_dict["Date"].append(tesla_date)
        tesla_dict["Revenue"].append(tesla_revenue)
In [104]:
tesla_revenue = pd.DataFrame(tesla_dict)
print(tesla_revenue.head())
         Date  Revenue
0  2022-09-30  $21,454
1  2022-06-30  $16,934
2  2022-03-31  $18,756
3  2021-12-31  $17,719
4  2021-09-30  $13,757
In [ ]:
 
In [109]:
tesla_revenue["Revenue"] = tesla_revenue['Revenue'].str.replace('$',"")
tesla_revenue["Revenue"] = tesla_revenue['Revenue'].str.replace(',',"")
print(tesla_revenue["Revenue"])
tesla_revenue.dropna(inplace=True)
0      21454
1      16934
2      18756
3      17719
4      13757
       ...  
157       28
158       21
159         
160       46
161       27
Name: Revenue, Length: 162, dtype: object
In [110]:
tesla_revenue =tesla_revenue[tesla_revenue['Revenue'] != ""]
In [111]:
tesla_revenue.tail()
Out[111]:
Date Revenue
156 2010-09-30 31
157 2010-06-30 28
158 2010-03-31 21
160 2009-09-30 46
161 2009-06-30 27
In [113]:
make_graph(tesla_data, tesla_revenue, "Tesla")
/tmp/ipykernel_369/109047474.py:5: UserWarning:

The argument 'infer_datetime_format' is deprecated and will be removed in a future version. A strict version of it is now the default, see https://pandas.pydata.org/pdeps/0004-consistent-to-datetime-parsing.html. You can safely remove this argument.

/tmp/ipykernel_369/109047474.py:6: UserWarning:

The argument 'infer_datetime_format' is deprecated and will be removed in a future version. A strict version of it is now the default, see https://pandas.pydata.org/pdeps/0004-consistent-to-datetime-parsing.html. You can safely remove this argument.

Question 3¶

In [86]:
gamestop = yf.Ticker("GME")
In [87]:
historical_gdata = gamestop.history(period="max")
In [88]:
gamestop_data = pd.DataFrame(historical_gdata)

gamestop_data.reset_index(inplace=True)

gamestop_data.head()
Out[88]:
Date Open High Low Close Volume Dividends Stock Splits
0 2002-02-13 00:00:00-05:00 1.620129 1.693350 1.603296 1.691667 76216000 0.0 0.0
1 2002-02-14 00:00:00-05:00 1.712707 1.716074 1.670626 1.683250 11021600 0.0 0.0
2 2002-02-15 00:00:00-05:00 1.683250 1.687458 1.658001 1.674834 8389600 0.0 0.0
3 2002-02-19 00:00:00-05:00 1.666418 1.666418 1.578047 1.607504 7410400 0.0 0.0
4 2002-02-20 00:00:00-05:00 1.615920 1.662209 1.603295 1.662209 6892800 0.0 0.0
In [89]:
game_url = "https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-PY0220EN-SkillsNetwork/labs/project/stock.html"
In [90]:
game_data= requests.get(game_url).text
In [91]:
soup_1 = BeautifulSoup(game_data, 'html.parser')
In [92]:
game_dict = {}
game_dict["Date"] = []
game_dict["Revenue"] = []
In [132]:
table_1 = soup_1.select("table")[1]
rows_1 = table_1.select("tbody > tr")

for row_1 in rows_1:
    col_1 = row_1.find_all("td")
    game_date = col_1[0].text
    game_revenue_ = col_1[1].text
    game_dict["Date"].append(game_date)
    game_dict["Revenue"].append(game_revenue_)
print(game_dict)
{'Date': ['2020-04-30', '2020-01-31', '2019-10-31', '2019-07-31', '2019-04-30', '2019-01-31', '2018-10-31', '2018-07-31', '2018-04-30', '2018-01-31', '2017-10-31', '2017-07-31', '2017-04-30', '2017-01-31', '2016-10-31', '2016-07-31', '2016-04-30', '2016-01-31', '2015-10-31', '2015-07-31', '2015-04-30', '2015-01-31', '2014-10-31', '2014-07-31', '2014-04-30', '2014-01-31', '2013-10-31', '2013-07-31', '2013-04-30', '2013-01-31', '2012-10-31', '2012-07-31', '2012-04-30', '2012-01-31', '2011-10-31', '2011-07-31', '2011-04-30', '2011-01-31', '2010-10-31', '2010-07-31', '2010-04-30', '2010-01-31', '2009-10-31', '2009-07-31', '2009-04-30', '2009-01-31', '2008-10-31', '2008-07-31', '2008-04-30', '2008-01-31', '2007-10-31', '2007-07-31', '2007-04-30', '2007-01-31', '2006-10-31', '2006-07-31', '2006-04-30', '2006-01-31', '2005-10-31', '2005-07-31', '2005-04-30', '2005-01-31', '2020-04-30', '2020-01-31', '2019-10-31', '2019-07-31', '2019-04-30', '2019-01-31', '2018-10-31', '2018-07-31', '2018-04-30', '2018-01-31', '2017-10-31', '2017-07-31', '2017-04-30', '2017-01-31', '2016-10-31', '2016-07-31', '2016-04-30', '2016-01-31', '2015-10-31', '2015-07-31', '2015-04-30', '2015-01-31', '2014-10-31', '2014-07-31', '2014-04-30', '2014-01-31', '2013-10-31', '2013-07-31', '2013-04-30', '2013-01-31', '2012-10-31', '2012-07-31', '2012-04-30', '2012-01-31', '2011-10-31', '2011-07-31', '2011-04-30', '2011-01-31', '2010-10-31', '2010-07-31', '2010-04-30', '2010-01-31', '2009-10-31', '2009-07-31', '2009-04-30', '2009-01-31', '2008-10-31', '2008-07-31', '2008-04-30', '2008-01-31', '2007-10-31', '2007-07-31', '2007-04-30', '2007-01-31', '2006-10-31', '2006-07-31', '2006-04-30', '2006-01-31', '2005-10-31', '2005-07-31', '2005-04-30', '2005-01-31'], 'Revenue': ['$1,021', '$2,194', '$1,439', '$1,286', '$1,548', '$3,063', '$1,935', '$1,501', '$1,786', '$2,825', '$1,989', '$1,688', '$2,046', '$2,403', '$1,959', '$1,632', '$1,972', '$3,525', '$2,016', '$1,762', '$2,061', '$3,476', '$2,092', '$1,731', '$1,996', '$3,684', '$2,107', '$1,384', '$1,865', '$3,562', '$1,773', '$1,550', '$2,002', '$3,579', '$1,947', '$1,744', '$2,281', '$3,693', '$1,899', '$1,799', '$2,083', '$3,524', '$1,835', '$1,739', '$1,981', '$3,492', '$1,696', '$1,804', '$1,814', '$2,866', '$1,611', '$1,338', '$1,279', '$2,304', '$1,012', '$963', '$1,040', '$1,667', '$534', '$416', '$475', '$709', '$1,021', '$2,194', '$1,439', '$1,286', '$1,548', '$3,063', '$1,935', '$1,501', '$1,786', '$2,825', '$1,989', '$1,688', '$2,046', '$2,403', '$1,959', '$1,632', '$1,972', '$3,525', '$2,016', '$1,762', '$2,061', '$3,476', '$2,092', '$1,731', '$1,996', '$3,684', '$2,107', '$1,384', '$1,865', '$3,562', '$1,773', '$1,550', '$2,002', '$3,579', '$1,947', '$1,744', '$2,281', '$3,693', '$1,899', '$1,799', '$2,083', '$3,524', '$1,835', '$1,739', '$1,981', '$3,492', '$1,696', '$1,804', '$1,814', '$2,866', '$1,611', '$1,338', '$1,279', '$2,304', '$1,012', '$963', '$1,040', '$1,667', '$534', '$416', '$475', '$709']}
In [137]:
game_revenue = pd.DataFrame(game_dict)
print(game_revenue.head())
         Date Revenue
0  2020-04-30  $1,021
1  2020-01-31  $2,194
2  2019-10-31  $1,439
3  2019-07-31  $1,286
4  2019-04-30  $1,548
In [138]:
game_revenue["Revenue"] = game_revenue['Revenue'].str.replace('$',"")
game_revenue["Revenue"] = game_revenue['Revenue'].str.replace(',',"")
In [139]:
game_revenue = game_revenue[game_revenue['Revenue'] != ""]
In [140]:
game_revenue.tail()
Out[140]:
Date Revenue
119 2006-01-31 1667
120 2005-10-31 534
121 2005-07-31 416
122 2005-04-30 475
123 2005-01-31 709

Question 5¶

In [133]:
def make_graph(stock_data, revenue_data, stock):
    fig = make_subplots(rows=2, cols=1, shared_xaxes=True, subplot_titles=("Historical Share Price", "Historical Revenue"), vertical_spacing = .3)
    stock_data_specific = stock_data[stock_data.Date <= '2021-06-14']
    revenue_data_specific = revenue_data[revenue_data.Date <= '2021-04-30']
    fig.add_trace(go.Scatter(x=pd.to_datetime(stock_data_specific.Date, infer_datetime_format=True), y=stock_data_specific.Close.astype("float"), name="Share Price"), row=1, col=1)
    fig.add_trace(go.Scatter(x=pd.to_datetime(revenue_data_specific.Date, infer_datetime_format=True), y=revenue_data_specific.Revenue.astype("float"), name="Revenue"), row=2, col=1)
    fig.update_xaxes(title_text="Date", row=1, col=1)
    fig.update_xaxes(title_text="Date", row=2, col=1)
    fig.update_yaxes(title_text="Price ($US)", row=1, col=1)
    fig.update_yaxes(title_text="Revenue ($US Millions)", row=2, col=1)
    fig.update_layout(showlegend=False,
    height=900,
    title=stock,
    xaxis_rangeslider_visible=True)
    fig.show()
    from IPython.display import display, HTML
    fig_html = fig.to_html()
    display(HTML(fig_html))
In [134]:
make_graph(tesla_data, tesla_revenue, "Tesla")
/tmp/ipykernel_369/109047474.py:5: UserWarning:

The argument 'infer_datetime_format' is deprecated and will be removed in a future version. A strict version of it is now the default, see https://pandas.pydata.org/pdeps/0004-consistent-to-datetime-parsing.html. You can safely remove this argument.

/tmp/ipykernel_369/109047474.py:6: UserWarning:

The argument 'infer_datetime_format' is deprecated and will be removed in a future version. A strict version of it is now the default, see https://pandas.pydata.org/pdeps/0004-consistent-to-datetime-parsing.html. You can safely remove this argument.

In [ ]:
make_graph(game_data, game_revenue, "gamestop")
In [ ]: